
# -*- coding: utf-8 -*-
from datetime import datetime
from sqlalchemy.orm import relationship, Mapped, mapped_column
from sqlalchemy import String, Integer, DateTime,Float,JSON,Time


from db.base import BaseModel


class Job(BaseModel):
    __tablename__ = "rpa_job"
    __table_args__ = ({"comment": "任务表"})
    
    task_id: Mapped[str] = mapped_column(String(64), nullable=False, unique=True, comment="任务uuid")
    task_name: Mapped[str] = mapped_column(String(255), nullable=False, comment="任务名称")
    task_type: Mapped[str] = mapped_column(String(32), nullable=False, comment="任务类型")
    jl_account: Mapped[str] = mapped_column(String(128), nullable=True, comment="巨量引擎账号")
    account_id: Mapped[str] = mapped_column(String(128), nullable=True, comment="千川账号或AD账号ID")
    max_threshold: Mapped[int] = mapped_column(Integer, nullable=True, comment='最大阈值100-200')
    promo_type: Mapped[str] = mapped_column(String(32), nullable=True, comment="推广目的")
    op_type: Mapped[str] = mapped_column(String(32), nullable=True, comment="操作类型")
    plan_id: Mapped[str] = mapped_column(String(128), nullable=True, comment="计划ID 或直播间名称 多个用逗号间隔")
    status: Mapped[str] = mapped_column(String(32), nullable=False, comment="任务状态")
    activation_code: Mapped[str] = mapped_column(String(64), nullable=False, comment="激活码")
    
     # 新增字段20250722
    statistics_strategy: Mapped[str] = mapped_column(String(32), nullable=True, comment="统计策略，如'智能统计'")
    customer_operation_count: Mapped[int] = mapped_column(Integer, nullable=True, comment="客户操作次数")
    operation_multiplier: Mapped[float] = mapped_column(Float, nullable=True, comment="操作倍数")
    operation_plan_types: Mapped[str] = mapped_column(JSON, nullable=True, comment="操作计划类型，多个用逗号分隔")
    plan_configs: Mapped[str] = mapped_column(JSON, nullable=True, comment="计划配置，JSON格式")
    white_list: Mapped[str] = mapped_column(String(500), nullable=True, comment="白名单")
    remark: Mapped[str] = mapped_column(String(255), nullable=True, comment="备注")

class Schedule(BaseModel):
    __tablename__ = "rpa_schedule"
    __table_args__ = ({"comment": "定时任务表"})
    
    schedule_id: Mapped[str] = mapped_column(String(64), nullable=False, unique=True, comment="定时任务uuid")
    name: Mapped[str] = mapped_column(String(64), nullable=False, comment="定时任务名称")
    execute_time: Mapped[datetime] = mapped_column(Time, nullable=False, comment="执行时间")
    task_ids: Mapped[str] = mapped_column(String(500), nullable=False, comment="任务IDS")
    activation_code: Mapped[str] = mapped_column(String(64), nullable=False, comment="激活码")

    @property
    def task_id_list(self):
        return self.task_ids.split(',')
    

class JobLogs(BaseModel):
    __tablename__ = "rpa_job_logs"
    __table_args__ = ({"comment": "任务日志表"})
    
    task_id: Mapped[str] = mapped_column(String(64), nullable=False, comment="任务ID")
    text: Mapped[str] = mapped_column(String(2000), nullable=False, comment="日志内容")
    running_time: Mapped[str] = mapped_column(String(32), nullable=False, comment="运行时长")
    